function getImageFullURLPath(avatar_url) {
    return "http://localhost:5500/rainf/src/main/resources/webapp/rainf" + avatar_url;
}

function setNavUserInfo(info) {
    $("#user-name").text(info.base.username);
    $("#avatar").attr("src",getImageFullURLPath(info.more.avatarUrl))
    $("#message a").attr("href","./user/message.html?id=" + info.base.id);
    $("#info a").attr("href","./user/info.html?id=" + info.base.id);
    $("#msg-item a").attr("href","./user/message.html?id=" + info.base.id);
    console.log("管理员")
    console.log(info.base.user_group_id)
    if(info.base.user_group_id === 2 || info.base.user_group_id === 3){
        $("#backend-login").css("display","");
    }

}

function checkNewNotice() {
    $.ajax({
        url: 'http://localhost:8080/notice/haveNew',
        method: 'POST',
        crossDomain: true,
        xhrFields: {withCredentials: true},
        data: {
            id: user.base.id
        },
        success: function (msg) {
            $("#msg-item span").remove();
            if(msg.data.count > 0) {
                $("#msg-item a").append("<span class='layui-badge layui-bg-blue'>" + msg.data.count + "</span>");
                let title = window.document.title;
                window.document.title = "(您有" + msg.data.count + "条新消息!)" + title;
            }
        }
    });
}

function msgClicked(){
    $("#msg-item span").remove();
    let title = window.document.title;
    let title_new = title.substring(title.indexOf(")") + 1);
    window.document.title = title_new;
}


/**
 * 检查用户是否登录，若登录，将右上角设置为用户的头像菜单，否则设置为登录按钮
 */
function checkLogin(){
    $.ajax({
        url: 'http://localhost:8080/login/checkLogin',
        method: 'POST',
        crossDomain: true,
        xhrFields: { withCredentials: true },
        success: function (msg) {
            let info = msg.data.user;
            //用户没有登录
            if(info == null){
                return;
            }
        //    登录了

            window.user = info;
        //    设置用户个人信息div内容，并显示
            checkNewNotice();

            setNavUserInfo(info);
        //    切换用户头像与登录按钮
            $("#login-nav-item").css("display","none");
            $("#personal-item").css("display","");
            //读取用户的消息，设置右上角新消息提醒


        }, error: function (error) {
            layer.msg("登录信息获取失败~")
        }
    });
}
function logout(){
    $.post({
        url: 'http://localhost:8080/login/logout',
        method: 'POST',
        crossDomain: true,
        xhrFields: { withCredentials: true },
        success: function (msg) {
            $("#login-nav-item").css("display","");
            $("#personal-item").css("display","none")
            window.location.reload();

        }, error: function (error) {
            layer.msg("数据获取失败~")
        }
    });
}